﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using ESC_POS_USB_NET.Enums;
using ESC_POS_USB_NET.Printer;
using Newtonsoft.Json.Linq;

namespace ShopayServer
{
    public class PrintTemplate
    {
        public Printer printer = new Printer("IBM00858");

        public void CashBox(string printName)
        {
            printer.PrintName(printName);
            printer.OpenDrawer();
            printer.PrintDocument();
            printer.Clear();
        }

        public void Print(string printName)
        {
            printer.PrintName(printName);
            printer.FullPaperCut();
            printer.PrintDocument();
            printer.Clear();
        }

        public void Text(string text)
        {
            printer.Append(text);
        }

        public void Bold()
        {
            printer.BoldMode(PrinterModeState.On);
        }

        public void BoldOff()
        {
            printer.BoldMode(PrinterModeState.Off);
        }

        public void Underline()
        {
            printer.UnderlineMode(PrinterModeState.On);
        }

        public void UnderlineOff()
        {
            printer.UnderlineMode(PrinterModeState.Off);
        }

        public void AlignLeft()
        {
            printer.AlignLeft();
        }

        public void AlignCenter()
        {
            printer.AlignCenter();
        }

        public void AlignRight()
        {
            printer.AlignRight();
        }

        public void Size(Int64 size)
        {
            printer.Size((int) size);
        }

        public void Font(Int64 font)
        {
            printer.FontMode((Fonts) font);
        }

        public void Width()
        {
            printer.NormalWidth();
        }

        public void Width2()
        {
            printer.DoubleWidth2();
        }

        public void Width3()
        {
            printer.DoubleWidth3();
        }

        public void LineHeight(Int64 height)
        {
            printer.SetLineHeight((byte) height);
        }

        public void Line()
        {
            printer.NewLine();
        }

        public void Separator(string speratorChar)
        {
            printer.Separator(speratorChar.ToCharArray()[0]);
        }

        public void BarCode(string code, Int64 width, Int64 height, Int64 show)
        {
            printer.Code128(code, (byte)width, (byte)height, (Positions)show);
        }

        public void QrCode(string text)
        {
            printer.QrCode(text);
        }

        public void QrCode1(string text)
        {
            printer.QrCode(text, QrCodeSize.Size1);
        }

        public void QrCode2(string text)
        {
            printer.QrCode(text, QrCodeSize.Size2);
        }

        public void Table(JArray item, JArray size)
        {
            Text(TableRow(item.ToObject<List<string>>().ToArray(), size.ToObject<List<int>>().ToArray()));
        }

        public string TableRow(string[] item, int[] size)
        {
            string content = "";
            bool over = true;

            for (int i = 0; i < item.Length; i++)
            {
                string tempitem = item[i];
                int tempsize = Math.Abs(size[i]);

                // 清空
                item[i] = "";
                // 如果长度大于预设，分割换行
                if (tempitem.Length > tempsize)
                {
                    item[i] = tempitem.Substring(tempsize);
                    tempitem = tempitem.Substring(0, tempsize);
                    over = false;
                }

                tempitem = size[i] > 0 ? tempitem.PadRight(tempsize) : tempitem.PadLeft(tempsize);

                content = content + tempitem;
            }

            if (over == false)
            {
                content = content + "\n" + TableRow(item, size);
            }

            return content;
        }
    }
}
